package com.vitabuybuy.tv.adaptebinding

import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import com.library.common.net.loadingtype.LoadingType
import com.library.common.util.DensityUtil
import com.library.common.widget.statuslayout.XLoadingView

/**
 * 作者：SimGa
 * 日期：2019-11-27
 * 时间：14:37
 */
object StatusViewAdapter {
    @BindingAdapter(value = ["viewStatus", "errorImage", "emptyImage", "errorContent", "emptyContent", "onRetryClick"], requireAll = false)
    @JvmStatic
    fun setStatusView(statuView: XLoadingView, viewStatus: Int,
                      errorImage: Int, emptyImage: Int,
                      errorContent: String?, emptyContent: String?,
                      onRetryClick: View.OnClickListener?) {
        val config = XLoadingView.init()
        when (viewStatus) {
            LoadingType.SHOW_LOADING -> {//加载框
                statuView.showLoading()
            }
            LoadingType.SHOW_EMPTY -> {//显示空数据文本
                if (emptyImage != 0) {
                    config.emptyImageResId = emptyImage
                }
                if (!TextUtils.isEmpty(emptyContent)) {
                    config.emptyStr = emptyContent
                }
                statuView.showEmpty()
            }
            LoadingType. SHOW_CONTENT -> {//显示正文内容
                statuView.showContent()
            }
            LoadingType.SHOW_ERROR -> {//显示加载错误
                if (errorImage != 0) {
                    config.errorImageResId = errorImage
                }
                if (!TextUtils.isEmpty(errorContent)) {
                    config.errorStr = errorContent
                }
                statuView.showError()
            }
//            SHOW_NETWORD -> {//显示网络错误
//                statuView.showNoNetwork()
//            }
        }
        statuView.setOnRetryClickListener(onRetryClick)
    }

    @BindingAdapter(value = ["isNotData"], requireAll = false)
    @JvmStatic
    fun setMargin(statuView: XLoadingView, isNotData: Boolean) {
        val lp = statuView.layoutParams as ViewGroup.MarginLayoutParams
        lp.bottomMargin = DensityUtil.dip2px(if (isNotData) 50f else 0f)
        lp.topMargin = DensityUtil.dip2px(if (isNotData) 50f else 0f)
        statuView.layoutParams = lp
    }
}
